package com.test.controller;

import com.test.entity.Const;
import com.test.entity.RestBean;
import com.test.entity.dto.Account;
import com.test.entity.dto.AccountDetails;
import com.test.entity.vo.request.AccountPrivacyStatusVo;
import com.test.entity.vo.request.ResetConfirmVo;
import com.test.entity.vo.request.ResetPwdVo;
import com.test.entity.vo.request.SaveAccountVo;
import com.test.entity.vo.response.AccountDetailsVo;
import com.test.entity.vo.response.AccountPrivacyVo;
import com.test.entity.vo.response.AccountVo;
import com.test.service.AccountDetailsService;
import com.test.service.AccountPrivacyService;
import com.test.service.AccountService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("api/user")
public class AccountController {
    @Resource
    AccountPrivacyService accountPrivacyService;
    @Resource
    AccountService accountService;
    @Resource
    AccountDetailsService accountDetailsService;
       @GetMapping("/info")
        public RestBean<AccountVo> info(Authentication authentication) {
           String username = authentication.getName();
           Account account = accountService.findByUsername(username);
           AccountVo vo = new AccountVo();
           BeanUtils.copyProperties(account,vo);
           return RestBean.success(vo);
       }
    @GetMapping("/details")
    public RestBean<AccountDetailsVo> details(@RequestAttribute(Const.VERIFY_USER_ID) int id) {
        log.info("用户id为:" + id);

        AccountDetails details = Optional
                .ofNullable(accountDetailsService.findByAccountId(id))
                .orElse(new AccountDetails()); // 确保 details 不为 null

        AccountDetailsVo vo = new AccountDetailsVo();

        // 使用自定义转换器处理 null 值
        BeanUtils.copyProperties(details, vo, getNullPropertyNames(details));

        return RestBean.success(vo);
    }

    private String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

       @PostMapping("/save_details")
      public RestBean<String> saveDetails(@RequestAttribute(Const.VERIFY_USER_ID) int id,
                                          @RequestBody @Valid SaveAccountVo vo) {
           Boolean result = accountDetailsService.save(id, vo);
           return result ? RestBean.success("保存成功") : RestBean.failure(400,"此用户名已经被使用，请切换其他用户名");
       }

       @PostMapping("/reset_email")
      public RestBean<String> resetEmail(@RequestAttribute(Const.VERIFY_USER_ID) int id,
                                         @RequestBody @Valid ResetConfirmVo vo) {
           String result = accountService.resetEmail(id, vo);
           return result == null ? RestBean.success() : RestBean.failure(400,result);
       }
       @PostMapping("/change_pwd")
      public RestBean<String> changePwd(@RequestAttribute(Const.VERIFY_USER_ID) int id,
                                         @RequestBody @Valid ResetPwdVo vo) {
           String result = accountService.resetPassword(id, vo);
           return result == null ? RestBean.success() : RestBean.failure(400,result);
       }
       @GetMapping("/get_privacy")
      public RestBean<AccountPrivacyVo> getPrivacy(@RequestAttribute(Const.VERIFY_USER_ID) int id) {
           AccountPrivacyVo vo = accountPrivacyService.getPrivacy(id);
           return RestBean.success(vo);
       }
       @PostMapping("/save_privacy")
      public RestBean<String> savePrivacy(@RequestAttribute(Const.VERIFY_USER_ID) int id,
                                         @RequestBody @Valid AccountPrivacyStatusVo vo) {
           accountPrivacyService.savePrivacy(id, vo);
           return RestBean.success();
       }




}
